package com.hyt.it.ogt.ks.controller;

import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.util.DateUtils;
import com.hyt.core.util.StringUtils;
import com.hyt.it.ogt.ks.config.ConfigManager;
import com.hyt.it.ogt.ks.enums.BizStatusCode;
import com.hyt.it.ogt.ks.feign.KwClient;
import com.hyt.it.ogt.ks.model.vo.ExamInfoLoginVO;
import com.hyt.it.ogt.ks.model.vo.PcClientDownAddressVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/ks/pc/")
@Api(tags = "10.PC客户端下载接口", value = "PC客户端下载接口")
@ApiSort(10)
@Slf4j
public class PcClientController {
    @Resource
    private ConfigManager configManager;
    @Resource
    private KwClient kwClient;

    @ApiOperation(value = "10.1.1 获取PC客户端下载地址", notes = "20000:成功", response = PcClientDownAddressVO.class)
    @ApiOperationSort(1)
    @GetMapping("getPcClientDownAddress")
    public ApiResponse getPcClientDownAddress() {
        return ApiResponse.builder().code(BizStatusCode.KS_SUCCESS_OK.getCode())
                .data(PcClientDownAddressVO.builder()
                        .winUrl(configManager.getWinDownloadUrl())
                        .winVersion(configManager.getWinVersion())
                        .macAppleUrl(configManager.getMacAppleDownloadUrl())
                        .macIntelUrl(configManager.getMacIntelDownloadUrl())
                        .macVersion(configManager.getMacVersion())
                        .qrCode(configManager.getWxDefaultQRCode())
                        .build()).build();
    }

    @ApiOperation(value = "10.1.2 查询考试是否存在", notes = "20000:成功", response = PcClientDownAddressVO.class)
    @ApiOperationSort(2)
    @GetMapping("searchExam")
    public ApiResponse<ExamInfoLoginVO> searchExam(String examId) {
        ApiResponse<ExamInfoLoginVO> apiResponse = ApiResponse.ConvertRet(kwClient.existExam(examId), ExamInfoLoginVO.class);
        ExamInfoLoginVO data = new ExamInfoLoginVO();
        if(Objects.nonNull(apiResponse) && Objects.equals(apiResponse.getCode(),BizStatusCode.KW_SUCCESS_OK.getCode())
                && Objects.nonNull(apiResponse.getData())){
            BeanUtils.copyProperties(apiResponse.getData(),data);
        }
        if(StringUtils.isEmpty(data.getExamId())){
            return ApiResponse.<ExamInfoLoginVO>builder().code(BizStatusCode.NOT_EXIST_EXAM.getCode()).data(data).build();
        }
        return ApiResponse.<ExamInfoLoginVO>builder().code(BizStatusCode.KS_SUCCESS_OK.getCode()).data(data).build();
    }

    @ApiOperation(value = "10.1.3 获取PC客户端升级地址", notes = "20000:成功", response = String.class)
    @ApiOperationSort(3)
    @GetMapping("getPcWinClientUpdateParameter")
    public void pcWinUpdate(HttpServletResponse response) {
        InputStream is = null;
        Map<String,String> map = new HashMap<String,String>();
        try {
            map.put("HOST","");
            map.put("LOCAL",configManager.getUpdateWinDownloadUrl());
            map.put("MD5",configManager.getUpdateWinFileMD5());	//16位
            map.put("SERVER", "");
            map.put("UPDATEFLAG", configManager.getUpdateWinFlag());	//0.不升级，1.升级
            map.put("UPDATETIMER", configManager.getUpdateWinTime());
            map.put("VER", configManager.getUpdateWinVersion());
            map.put("LPwd",configManager.getUpdateWinPassword());	//解屏密码
            map.put("SeverDate", DateUtils.formatDate(new Date()));
            map.put("OPC",configManager.getUpdateWinOpc());		//OPC：是否启用操作码，正式考试=1，模拟/仿真测试=0
            map.put("PNList",configManager.getUpdateWinPnList());
            map.put("PCList",configManager.getUpdateWinPcList());
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(JSONObject.toJSONString(map));
            response.getWriter().close();
        }  catch (Exception e) {
            log.error("获取客户端（win）参数失败！");
        }
    }


    @ApiOperation(value = "10.1.4 获取深港澳PC客户端下载地址", notes = "20000:成功", response = PcClientDownAddressVO.class)
    @ApiOperationSort(1)
    @GetMapping("getShengangaoPcClientDownAddress")
    public ApiResponse getShengangaoPcClientDownAddress() {
        return ApiResponse.builder().code(BizStatusCode.KS_SUCCESS_OK.getCode())
                .data(PcClientDownAddressVO.builder()
                        .winUrl(configManager.getSgaDownloadUrl())
                        .winVersion(configManager.getWinVersion())
                        .macAppleUrl(configManager.getMacSgaAppleDownloadUrl())
                        .macIntelUrl(configManager.getMacSgaIntelDownloadUrl())
                        .macVersion(configManager.getMacVersion())
                        .qrCode(configManager.getWxDefaultQRCode())
                        .build()).build();
    }
}
